package user

import (
	"time"

	"gorm.io/gorm"
)

// User 用户模型
type User struct {
	ID        uint           `json:"id" gorm:"primaryKey;autoIncrement" comment:"用户ID"`
	Username  string         `json:"username" gorm:"size:50;unique;not null" binding:"required" comment:"用户名"`
	Nickname  string         `json:"nickname" gorm:"size:50" comment:"昵称"`
	Email     string         `json:"email" gorm:"size:100" comment:"邮箱"`
	Phone     string         `json:"phone" gorm:"size:20" comment:"手机号"`
	Password  string         `json:"-" gorm:"size:255;not null" comment:"密码"`
	Avatar    string         `json:"avatar" gorm:"size:255" comment:"头像"`
	Status    int            `json:"status" gorm:"default:1;not null" comment:"状态(1:启用 0:禁用)"`
	LastLogin *time.Time     `json:"last_login" comment:"最后登录时间"`
	CreatedAt time.Time      `json:"created_at" gorm:"autoCreateTime" comment:"创建时间"`
	UpdatedAt time.Time      `json:"updated_at" gorm:"autoUpdateTime" comment:"更新时间"`
	DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index" comment:"删除时间"`

	// 用户角色关系通过 user_roles 表手动管理，不使用 GORM 关联
}

// TableName 设置表名
func (User) TableName() string {
	return "users"
}

// Role 角色模型（简化版，用于关联）
type Role struct {
	ID        uint           `json:"id" gorm:"primaryKey"`
	Name      string         `json:"name"`
	DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
}

// UserRole 用户角色关联模型
type UserRole struct {
	UserID uint `json:"user_id" gorm:"primaryKey"`
	RoleID uint `json:"role_id" gorm:"primaryKey"`
	User   User `json:"user" gorm:"foreignKey:UserID;references:ID"`
	Role   Role `json:"role" gorm:"foreignKey:RoleID;references:ID"`
}

// TableName 设置表名
func (UserRole) TableName() string {
	return "user_roles"
}

// CreateRequest 创建用户请求
type CreateRequest struct {
	Username string `json:"username" binding:"required" comment:"用户名"`
	Nickname string `json:"nickname" comment:"昵称"`
	Email    string `json:"email" binding:"" comment:"邮箱"`
	Phone    string `json:"phone" comment:"手机号"`
	Password string `json:"password" binding:"required,min=6" comment:"密码"`
	Avatar   string `json:"avatar" comment:"头像"`
	Status   *int   `json:"status" comment:"状态"`
	RoleIDs  []uint `json:"role_ids" comment:"角色ID列表"`
}

// UpdateRequest 更新用户请求
type UpdateRequest struct {
	Username *string `json:"username" comment:"用户名"`
	Nickname *string `json:"nickname" comment:"昵称"`
	Email    *string `json:"email" binding:"" comment:"邮箱"`
	Phone    *string `json:"phone" comment:"手机号"`
	Password *string `json:"password" binding:"omitempty,min=6" comment:"密码"`
	Avatar   *string `json:"avatar" comment:"头像"`
	Status   *int    `json:"status" comment:"状态"`
	RoleIDs  []uint  `json:"role_ids" comment:"角色ID列表"`
}

// QueryRequest 查询用户请求
type QueryRequest struct {
	Page     int    `json:"page" form:"page" binding:"min=1"`
	PageSize int    `json:"page_size" form:"page_size" binding:"min=1,max=100"`
	Keyword  string `json:"keyword" form:"keyword"`
	Username string `json:"username" form:"username"`
	Nickname string `json:"nickname" form:"nickname"`
	Email    string `json:"email" form:"email"`
	Phone    string `json:"phone" form:"phone"`
	Status   *int   `json:"status" form:"status"`
}

// UserWithRoles 带角色信息的用户
type UserWithRoles struct {
	ID        uint       `json:"id"`
	Username  string     `json:"username"`
	Nickname  string     `json:"nickname"`
	Email     string     `json:"email"`
	Phone     string     `json:"phone"`
	Avatar    string     `json:"avatar"`
	Status    int        `json:"status"`
	LastLogin *time.Time `json:"last_login"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	Roles     []Role     `json:"roles" gorm:"-"`
}

// ResetPasswordRequest 重置密码请求
type ResetPasswordRequest struct {
	NewPassword string `json:"new_password" binding:"required,min=6" comment:"新密码"`
}
